package com.example.demo.service.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.CompetitionScoreMapper;
import com.example.demo.pojo.Competitions.CompetitionScore;
import com.example.demo.pojo.Competitions.CompetitionScoreDTO;
import com.example.demo.pojo.R;
import com.example.demo.service.CompetitionScoreService;
import com.example.demo.utils.TokenUtil.decryptToken;
import com.example.demo.utils.TokenUtil.spiltUtils;
import com.example.demo.utils.UserUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CompetitionScoreServiceImpl extends ServiceImpl<CompetitionScoreMapper, CompetitionScore> implements CompetitionScoreService {


    @Resource
    private CompetitionScoreMapper competitionScoreMapper;

    @Resource
    private UserUtil userUtil;


    @Override
    public R<List<CompetitionScoreDTO>> getCompetitionScoreById(String token, Integer id) {
        //获取当前用户的id
        String decrypt = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(decrypt);
        Integer userId = userUtil.getUserId(userName);
        //判断是否实名
        boolean realNameAuthentication = userUtil.getRealNameAuthentication(userId);
        if (!realNameAuthentication) {
            return R.failed("你还未进行实名认证！");
        }
        List<CompetitionScoreDTO> competitionScoreDTOS = competitionScoreMapper.selectCompetitionScoresList(id);
        if (competitionScoreDTOS != null) {
            return R.success(competitionScoreDTOS);
        }
        return R.failed("还没有生成排名!");
    }
}
